package com.scitc.pleasurevideo.util;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

public class SearchTvUtil {

    private static OnSecrchName onSecrchName;

    public interface OnSecrchName {
        void setOnSearchName(String name);

        void setOnSearchImg(String url);
    }

    public static void setOnSecrchName(OnSecrchName imgurl) {
        onSecrchName = imgurl;
    }

    public static void searchTvName(final Context context, final String name) {
        String url = "https://search.douban.com/movie/subject_search?cat=1002&search_text=" + name;
        OkGo.<String>get(url)
                .tag(context)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        //先判断搜索到没有
                        int flen = response.body().indexOf("https://img3.doubanio.com/view/photo/s_ratio_poster/public/");
                        if (flen < 0) {
                            onSecrchName.setOnSearchName(null);
                            return;
                        }

                        int elen = response.body().indexOf("alt=", flen);

                        String tempstring = response.body().substring(flen, elen - 2);

                        onSecrchName.setOnSearchName(tempstring);
                    }
                });


    }

    public static void searchTvImg(final Context context, final String url) {

        OkGo.<String>get(url)
                .tag(context)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        //判断找到img没有
                        int flen = response.body().indexOf("<div class=\"vodImg\">");
                        if (flen < 0) {
                            return;
                        }
                        flen = response.body().indexOf("src=", flen);
                        int elen = response.body().indexOf("alt=", flen);

                        String imgstring = response.body().substring(flen + 5, elen - 2);

                        if (imgstring.indexOf("http") < 0) {
                            imgstring = "https://img.sokoyo-rj.com/tuku" + imgstring;
                        }

                        onSecrchName.setOnSearchImg(imgstring);
                    }
                });

    }


}
